package com.lagou.educourseboot.controller;

import com.lagou.educourseboot.entity.Course;
import com.lagou.educourseboot.entity.CourseDTO;
import com.lagou.educourseboot.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("course")
@CrossOrigin//跨域
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("getAllCourse")
    public List<CourseDTO> getAllCourse(){
        // 模拟多线程：创建一个容量20个的线程池
        ExecutorService es = Executors.newFixedThreadPool(20);
        // 模拟20个线程同时查询
        for (int i = 0;i <20;i++){
            es.submit (new Runnable () {
                @Override
                public void run() {
                    courseService.getAllCourse ();
                }
            });
        }
        return courseService.getAllCourse ();
    }

    @GetMapping("getCoursesByUserId")
    public List<CourseDTO> getCoursesByUserId( Integer userid) {
        System.out.println("userid = " + userid);
        return courseService.getCoursesByUserId(userid);
    }

    @GetMapping("getCourseById/{courseid}")
    public CourseDTO getCourseById(@PathVariable("courseid")Integer courseid){
        CourseDTO course = courseService.getCourseById (courseid);
        return course;
    }
}
